/****************************************************************************************
 *            Main Task File
 ****************************************************************************************/

#include <includes.h>
#include <system_LPC17xx.h>
#include <LPC17xx.h>
#include <at45db161.h>

uint8_t AT45_TestBuf[512];

void Main_Task(void * p_arg)
{
    OS_ERR err;
    uint16_t Cnt;

    p_arg = p_arg;
    
	AT45_SPIInit();
	
	AT45_MainMemRead(512 * 4, 512, AT45_TestBuf);
	Cnt = 512;
	
	if( (AT45_StatusRead() & AT45_ST_PAGE_SIZE_MASK ) == AT45_ST_PAGE_SIZE_528)
		AT45_SendCMD(CODE_POWER_OF_2);
//	AT45_SendCMD(CODE_ENABLE_SECTOR_PROTECTION);
	Cnt = AT45_StatusRead();
	
	Cnt = 512;
	while(Cnt--)
		AT45_TestBuf[Cnt] = Cnt;
	
//	AT45_Write(CMD_MAIN_PROG_THROUGH_BUF1, 512*16, 512, AT45_TestBuf);
//	AT45_Write(CMD_MAIN_PROG_THROUGH_BUF1, 512*17, 512, AT45_TestBuf);
	
	Cnt = 512;
	while(Cnt--)
		AT45_TestBuf[Cnt] = Cnt + 100;
	
//	AT45_MainMemWrite(512*16 + 300, 400, AT45_TestBuf);
	
	Cnt = 512;
	while(Cnt --)
		AT45_TestBuf[Cnt] = 0;
	AT45_MainMemRead(512 * 16, 512, AT45_TestBuf);
	
	Cnt = 512;
	while(Cnt --)
		AT45_TestBuf[Cnt] = 0;
	AT45_MainMemRead(512 * 17, 512, AT45_TestBuf);
	
	Cnt = 512;
	
//	AT45_Write(CMD_BUF1_WRITE, 0, 512, AT45_TestBuf);
	
	
	Cnt = 512;
	while(Cnt --)
		AT45_TestBuf[Cnt] = 0;
	
	AT45_Read(CMD_BUF1_READ, 0, 512, AT45_TestBuf);
	Cnt = 512;
	
//	AT45_SendCMDWithAddr(CMD_BUF1_TO_MAIN_INERASE, 512 * 4);
	Cnt = 512;
	
	Cnt = 512;
	while(Cnt --)
		AT45_TestBuf[Cnt] = 0;
	
	AT45_MainMemRead(512 * 4, 512, AT45_TestBuf);
	
//	AT45_Write(CMD_MAIN_PROG_THROUGH_BUF1, 512*8, 512, AT45_TestBuf);
	Cnt = 512;
	
	
	while(Cnt --)
		AT45_TestBuf[Cnt] = 0;
	
	AT45_MainMemRead(512 * 8, 512, AT45_TestBuf);
    Cnt = 512;
	
    while(1)
    {
        OSTimeDly(200,OS_OPT_TIME_DLY,&err);
    }
}
